/**
 * Copyright 厦门中软海晟信息技术有限公司 版权所有 违者必究 2019
 */
package com.opencvjava.lessons.core;

import org.opencv.core.Mat;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;

import static com.opencvjava.support.util.CvUtils.lutMIM;
import static com.opencvjava.support.util.CvUtils.mat;
import static org.opencv.core.Core.getTickCount;
import static org.opencv.core.Core.getTickFrequency;
/**
 * @author : sunzb(sunzb@hsit.com.cn)
 * @date: 2019/1/3
 */
@Service
public class L3_Scan_Lookup_TimeMeasure {
    public void test() {
        // time measure
        double t = getTickCount();
        for (int i = 0; i < 100; i++) {
            try {
                Thread.sleep(10);
            } catch (Exception ex) {
                throw new RuntimeException();
            }
        }
        t = (getTickCount() - t) / getTickFrequency();
        System.out.println("time elapse : " + t);
        // scan
        Mat src = mat("lena.jpg");
        int rows = src.rows();
        int cols = src.cols();
        int channels = src.channels();
        byte[] data = new byte[rows * cols * channels];
        src.get(0, 0, data);
        int index;
        int gray, b, g, r;
        for (int row = 0; row < rows; row++) {
            for (int col = 0; col < cols; col++) {
                index = row * cols * channels + col * channels;
                if (channels == 1) {
                    gray = data[index] & 0xff;
                } else if (channels == 3) {
                    b = data[index] & 0xff;
                    g = data[index + 1] & 0xff;
                    r = data[index + 2] & 0xff;
                }
            }
        }
        // look up
        Map<Integer, Integer> lookUpTable = new HashMap<>();
        for (int i = 0; i < 256; i++) {
            lookUpTable.put(i, 255 - i);
        }
        Mat dst = new Mat(src.size(), src.type());
        lutMIM(src, dst, lookUpTable);
    }
}
